package com.timothy.android.chart;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.chart.model.BaseBean;
import com.timothy.android.chart.model.ChoiceResult;
import com.timothy.android.chart.model.PageBaseBean;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class BaseChartActivity extends Activity implements OnDoubleTapListener, OnGestureListener,OnTouchListener{

	private final static String LOG_TAG = "BaseChartActivity";
	
	//header components
	protected TextView answerDesc;
	protected TextView priorityList = null;
	
	//popupWindow components
	protected PopupWindow popupWindow;
	protected ListView lv_group;
	protected List<String> groups;
	protected Integer pressedItemIndex = -1;
	protected View popupView;
	
	protected void showWindow(View parent) {
		Log.i(LOG_TAG, "showWindow()...");
		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			popupView = layoutInflater.inflate(R.layout.group_list, null);
			lv_group = (ListView) popupView.findViewById(R.id.lvGroup);
			// 加载数据
			groups = new ArrayList<String>();
			
			/*Map<Integer, List<ChoiceResult>> priorityMap = priorityStatistics.getPriorityMap();
			Iterator<Integer> it = priorityMap.keySet().iterator();
			while (it.hasNext()) {
				Integer key = it.next();
				groups.add("Priority " + String.valueOf(key));
			}*/
			
			//test data
			groups.add("2DBar-Ver");
			groups.add("2DBar-Hon");
			groups.add("3DBar-Ver");
			groups.add("3DBar-Hon");
			groups.add("Cylinder-Ver");
			groups.add("Cylinder-Hon");
			
			Log.i(LOG_TAG, "pressedItemIndex:"+pressedItemIndex);
			GroupAdapter groupAdapter = new GroupAdapter(this, groups);
			lv_group.setAdapter(groupAdapter);
			// 创建一个PopuWidow对象
			popupWindow = new PopupWindow(popupView, 300, 350);
		}else{
			popupWindow.dismiss();
		}

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth() / 2;
		Log.i(LOG_TAG, "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 0);
		lv_group.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				Log.i(LOG_TAG, "onItemClick()...");
//				Toast.makeText(PopupWindowDemoActivity.this,groups.get(position), 1000).show();
				//initial all items background color
        		for(int i=0;i<adapterView.getChildCount();i++){
        			View item = adapterView.getChildAt(i);
        			item.setBackgroundColor(Color.parseColor("#4C4C4C"));
        		}
				view.setBackgroundColor(Color.parseColor("#D5E43C"));
				pressedItemIndex = position;
				Log.i(LOG_TAG, "pressedItemIndex:" + pressedItemIndex);
				if (popupWindow != null) {
					popupWindow.dismiss();
				}
				
//				SimpleDraw();
				
				Log.i(LOG_TAG, "onItemClick() END");
			}
		});
	}

	//data bean
	protected BaseBean baseBean = null;
	protected final static int SCREEN_SIZE = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
	    //set test data
		List<PageBaseBean> pageList = new ArrayList<PageBaseBean>();
		PageBaseBean page = new PageBaseBean();
		ChoiceResult cr1 = new ChoiceResult(1,50,"Yes","Yes");
		cr1.setShare(1.0);
		page.addChoiceResult(cr1);
		ChoiceResult cr2 = new ChoiceResult(2,50,"No","No");
		cr2.setShare(0.0);
		page.addChoiceResult(cr2);
		page.setPage(1);
		pageList.add(page);
		
		baseBean = new BaseBean("Are you agree?",1,8,2,1,pageList);
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
